package com.grad.webstudy.controller;

import com.github.pagehelper.PageInfo;
import com.grad.webstudy.annota.ApiAuthorization;
import com.grad.webstudy.annota.LoginAuthorization;
import com.grad.webstudy.constants.AuthConstants;
import com.grad.webstudy.context.UserContext;
import com.grad.webstudy.enums.ObjTypeEnums;
import com.grad.webstudy.model.User;
import com.grad.webstudy.service.ApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

/**
 * @Description:
 * @Author: zjf
 * @Date: 2020/2/5 15:21.
 */

@RestController
@RequestMapping("/apply")
@Validated
public class ApplyController {

    @Autowired
    private ApplyService applyService;

    /**
     * 为用户申请身份时所用的接口
     *
     * @return
     */
    @PostMapping("/addApply")
    @LoginAuthorization
    public boolean addApply(String applyDesc) {
        User loginUser = UserContext.getLoginUser();
        return applyService.addApply(ObjTypeEnums.USER.getObjType(), loginUser.getId(),applyDesc);
    }


    @DeleteMapping("/undo")
    @LoginAuthorization
    public boolean undoApply(int id) {
        return applyService.undoApply(id);
    }


    @PutMapping("/changeApply")
    @ApiAuthorization(auth = AuthConstants.APPLY_UPDATE)
    public boolean changeApply(int id,
                               @Max(5) @Min(1) @RequestParam(required = true) int status,
                               String result) {
        return applyService.ChangeApply(id, status, result);
    }


    @GetMapping("/queryByPageForAdmin")
    @ApiAuthorization(auth = AuthConstants.APPLY_QUERY)
    public PageInfo queryApplyByPageForAdmin(@RequestParam(defaultValue = "1") int currPage,
                                             @RequestParam(defaultValue = "5") int pageSize,
                                             Integer status) {
        return applyService.queryApplyByPageForAdmin(currPage, pageSize, status);
    }

    @GetMapping("/queryByPageForEveryOne")
    @LoginAuthorization
    public PageInfo queryApplyByPageForEveryOne(@RequestParam(defaultValue = "1") int currPage,
                                                @RequestParam(defaultValue = "5") int pageSize,
                                                Integer status) {
        return applyService.queryApplyByPageForEveryOne(currPage, pageSize, status);
    }
}
